<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
   <link rel="stylesheet" href="text-css.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body>
<div  class="box" style="display:-webkit-flex;display:flex;-webkit-flex-direction:column;flex-direction:column;
">
    <h1><a href="equip-http-2-add-0">设备数据HTTP/2方式推送</a></h1>
    <p>更新时间：2018-12-21 13:28:08</p>
   <p>HTTP/2转储功能适用于飞燕平台与企业服务器之间的大量消息流转。通过集成和使用HTTP/2 SDK，即可实现身份认证、消息接收的能力。
   </p>
    <img src="img/1534215963484-e5b56548-c40b-48d9-8287-738a98cf4efb_副本.png" alt=" " style="width:100%">
    <h1><a name="equip-http-2-add-1">1.HTTP/2SDK使用</a></h1>
    <h2><a name="equip-http-2-add-1-1">1.1依赖引入</a></h2>
    <p>在项目中添加maven依赖，maven信息如下</p>
    <pre class="pre-box">

&ltdependency>
   &ltgroupId>com.hsd.voice&lt/groupId>
    &ltartifactId>voice-client&lt/artifactId>
    &ltversion>0.0.1 &lt/version>
&lt/dependency>

    </pre>
    <h2><a name="equip-http-2-add-2-1">1.2身份认证</a></h2>
    <p>身份认证需要使用AppKey和AppSecret，该信息可以从控制台中获取，如图所示：</p><br>
    <img src="img/124_20190509205610.png" alt="" style="width:100%">
    <br>
    <p>建连时使用MessageClient.connect(Profile, MessageCallback)方法，Profile需要提供EndPoint，AppKey和AppSecret用于鉴权，MessageCallback为默认的消息回调，用于接收消息。
        示例如下：</p>
    <pre class="pre-box">

Profile profile = Profile.getAppKeyProfile("${END_POINT}",
  "${APP_KEY}", "${APP_SECRET}");

MessageCallback messageCallback = new MessageCallback() {
    public Action consume(Message message) {
        System.out.println("receive : " +
        new String(message.getPayload()));
        return Action.CommitSuccess;
    }
};
    </pre>
    <h2><a name="equip-http-2-add-1-3">1.3消息接受</a></h2>
    <p>连接建立后，服务端会立即向SDK推送已订阅的消息，因此建连时需要提供默认消息接收接口，用于处理未设置回调的消息。建议在connect之前，调用setMessageListener设置消息回调。
        默认消息回调设置示例如下：</p>
    <pre class="pre-box">

MessageClient messageClient = MessageClientFactory.messageClient(profile);
messageClient.setMessageListener(messageCallback);
    </pre>
    <h4>消息接收接口</h4>
    <p>用户需要实现MessageCallback接口的consume方法，并调用client的setMessageListener()方法。
        代码片段示例如下：</p>
    <pre class="pre-box">

 MessageCallback messageCallback = new MessageCallback() {
    public Action consume(Message message) {
        System.out.println("receive : " +
        new String(message.getPayload()));
        return Action.CommitSuccess;
    }
};

messageClient.setMessageListener(messageCallback);       
    </pre>
    <h4>指定Topic回调</h4>
    <p>对于消息接收的回调，可以指定带通配符的Topic，如/${ProductKey}/{$DeviceName}/#，指定Topic的回调优先级高于通用的回调，一条消息匹配到多个Topic时，按字典序优先调用，仅会回调一次。
        示例如下：</p>
    <pre class="pre-box">

// 当收到消息的Topic匹配到"/Product_A/Device_B/update"时，会优先调用该回调
messageClient.setMessageListener("/Product_A/Device_B/#",messageCallback);
    </pre>
    <h4>通用回调</h4>
    <p>设置该回调后，对于未设置指定的topic回调的消息，均会调用该回调。示例如下：</p>
    <pre class="pre-box">

// 当收到消息的topic未匹配到已指定的Topic时，调用该回调
messageClient.setMessageListener(messageCallback);
    </pre>
    <h2><a name="equip-http-2-add-1-4">1.4完整示例</a></h2>
    <pre class="pre-box">

 public static void main(String[] args) throws UnsupportedEncodingException {
    Profile profile = Profile.getAppKeyProfile("要连接的&ltEND_POINT>",
        "你的&ltAppKey>", "你的&ltAppSecret>");
    MessageCallback messageCallback = new MessageCallback() {
        public Action consume(Message message) {
            System.out.println("receive : " +
        new String(message.getPayload()));
            return Action.CommitSuccess;
        }
    };

    MessageClient messageClient = MessageClientFactory.messageClient(profile);
    messageClient.setMessageListener(messageCallback);
    messageClient.connect(messageCallback);

    try
    {
        System.in.read();
    } catch (Exception e) {}
}
    </pre>
    <h2><a name="equip-http-2-add-1-5">1.5连接节点信息</a></h2>
    <table border="1">
        <tr><th style="width:10%">区域</th><th style="width:90%">End point</th></tr>
        <tr><td>国内</td><td><a href="https://ilop.iot-as-http2.cn-shanghai.aliyuncs.com:443" style="color:blue">N/A</a></td></tr>
    </table>
    <h2><a name="equip-http-2-add-1-6">1.6消息格式</a></h2>
    <h3>(1) 设备上报文本消息</h3>
    <h3>消息字段说明：</h3>
    <table border="1" class="table table-striped table-bordered">
        <tr><th style="30%">参数</th><th style="width:10%">类型</th><th style="width:60%">含义</th></tr>
        <tr><td>deviceKey</td><td>String</td><td>设备的key值</td></tr>
        <tr><td>userId</td><td>String</td><td>用户的id</td></tr>
        <tr><td>test</td><td>String</td><td>设备上的文本消息</td></tr>
         </table>
    <h3>消息范例：</h3>
    <pre class="pre-box">

{
        "deviceKey":"test_device_key",
        "userId":"0300MSKL03667c544f69342a74Sv4Za4",
        "text":"设备上报文本"
}

    </pre>
    
    <h3>(2) 设备语音数据流</h3>
    <h3>消息字段说明：</h3>
    <table border="1" class="table table-striped table-bordered">
        <tr><th style="30%">参数</th><th style="width:10%">类型</th><th style="width:60%">含义</th></tr>
        <tr><td>deviceKey</td><td>String</td><td>设备的key值</td></tr>
        <tr><td>userId</td><td>String</td><td>用户的id</td></tr>
        <tr><td>content</td><td>byte[]</td><td>语音数据</td></tr>
    </table>
    <h3>消息范例：</h3>
    <pre class="pre-box">

{
        "deviceKey":"test_device_key",
        "userId":"0300MSKL03667c544f69342a74Sv4Za4",
        "content":[10,20,30.....]
    }
}        
    </pre>

</div>
<div id="intro-platform-nav" style="position:absolute;right:100px;top:150px;width:30px;height:30px;position:fixed;z-index:1;border:1px solid #CCC;box-shadow:4px 4px 10px #E8E8E8;border-radius:10px;background:#F5F5F5	">
    <img src="img\\sanheng.png" alt="" style="width:30px;height:30px;position:fixed;position:absolute;right:0px;top:0px;">
    <div  id="nav-text" style="display:none;line-height:15px;overflow:scroll;height:250px;overflow-x:hidden;padding-left:15px;margin-top:15px">
        <p><a href="#equip-http-2-add-0">设备数据HTTP/2方式推送</a></p>
        <p><a href="#equip-http-2-add-1">1.HTTP/2SDK使用</a></p>
        <p><a href="#equip-http-2-add-1-1">1.1依赖引入</a></p>
        <p><a href="#equip-http-2-add-1-2">1.2身份认证</a></p>
        <p><a href="#equip-http-2-add-1-3">1.3消息接受</a></p>
        <p><a href="#equip-http-2-add-1-4">1.4完整示例</a></p>
        <p><a href="#equip-http-2-add-1-5">1.5链接节点信息</a></p>
        <p><a href="#equip-http-2-add-1-6">1.6消息格式</a></p>
    </div>
</div>
</div>
</body>
<script src="jquery-3.3.1.min.js"></script>
<script>
    $(document).ready(function(){
      $("#intro-platform-nav").click(function(){
       $("#intro-platform-nav").animate({
            "width":"360px",
            "height":"250px"
       },600);
       $("#nav-text").css("display","");
    });
    $("#intro-platform-nav").mouseleave(function(){
      $("#intro-platform-nav").animate({
      "width":"30px",
      "height":"30px"
      },500);
      $("#nav-text").css("display","none");
    });
   })
</script>

</html>